<template>
  <div>
    <el-card class="box-card">
      <!-- 头部区域 -->
      <template #header>
        <div class="header">
          <span>仓库管理</span>
          <div class="extra">
            <el-button
              icon="el-icon-plus"
              type="primary"
              @click="drawer = true"
              >添加仓库信息</el-button
            >
          </div>
        </div>
      </template>
      <!-- 搜索表单 -->
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item label="仓库名：">
          <el-input
            v-model="SearchStore.storehouseName"
            placeholder="仓库名"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" type="primary" @click="getStore"
            >查询</el-button
          >
          <el-button icon="el-icon-refresh" type="primary" @click="reset"
            >重置</el-button
          >
        </el-form-item>
      </el-form>

      <!-- 商品管理列表 -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column label="仓库名" prop="storehouseName">
        </el-table-column>
        <el-table-column label="仓库管理员" prop="realName"> </el-table-column>
        <el-table-column label="联系电话" prop="phone"> </el-table-column>
        <el-table-column label="位置" prop="location"> </el-table-column>
        <el-table-column label="备注" prop="remark"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              slot="reference"
              circle
              icon="el-icon-edit"
              size="small"
              type="primary"
              @click="edit(scope.row)"
            ></el-button>

            <el-button
              slot="reference"
              circle
              icon="el-icon-delete"
              size="small"
              type="danger"
              @click="DelStore(scope.row.storehouseId)"
            ></el-button>
          </template>
        </el-table-column>
        <template #empty>
          <el-empty description="没有数据" />
        </template>
      </el-table>

      <!-- 分页条 -->
      <div class="block">
        <el-pagination
          :current-page="this.pageNum"
          :page-size="this.pageSize"
          :page-sizes="[2, 3, 5, 10]"
          :total="this.total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        >
        </el-pagination>
      </div>
      <!-- 抽屉 -->
      <el-drawer
        :direction="direction"
        :title="title"
        :visible.sync="drawer"
        class="addform"
      >
        <el-form ref="form" :model="store" label-width="80px">
          <el-form-item label="仓库名：">
            <el-input v-model="store.storehouseName"></el-input>
          </el-form-item>
         
          <el-form-item label="位置：">
            <el-input v-model="store.location"></el-input>
          </el-form-item>
         
          <el-form-item label="备注:">
            <el-input v-model="store.remark"></el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="onSubmit">立即创建</el-button>
            <el-button @click="drawer = false">取消</el-button>
          </el-form-item>
        </el-form>
      </el-drawer>
    </el-card>
  </div>
</template>

<script>
import request from '@/utils/request';

export default {
  data() {
    return {
       // //从本地存储获取user数据
       user: localStorage.getItem("user")
        ? JSON.parse(localStorage.getItem("user"))
        : {},
      store:{
        storehouseId:'',
        storehouseName:'',
        userId:'',
        location:'',
        remark:''
      },
      SearchStore: {
        storehouseName: "",
      },
      drawer: false,
      direction: "rtl",
      title: "新增仓库信息",
      tableData: [],
      pageNum: 1,
      pageSize: 3,
      total: 10,
    };
  },
  methods: {
    //编辑方法
    edit(obj) {
      this.title = "修改课程",
        this.store = obj;
      this.drawer = true

    },
     //删除
     DelStore(id) {
      this.$confirm('是否删除这个仓库信息?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        request.delete("/storehouse/DelStore", {
          params: {
            Id: id
          }
        }).then(res => {
          if (res.data.code == '0') {
            this.$message.success("删除成功")
            this.getStore()
          } else {
            this.$message.error("删除失败")
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    //清理store
    cleanstore(){
       this.store.storehouseName='',
       this.store.location='',
       this.store.remark=''
    },
     //新增
     onSubmit() {
      request.post("/storehouse/AddOrUpdate", this.store).then(res => {
        if (res.data.code == '0') {
          this.$message.success("添加成功")
          this.drawer = false;
          this.cleanstore();
          this.getStore();
        } else {
          this.$message.error("添加失败")
        }
      })
    },
     //模糊查询
     getStore(){
      request.get("/storehouse/SearchStore",{params:{
        pageNum:this.pageNum,
        pageSize:this.pageSize,
        storehouseName:this.SearchStore.storehouseName
      }}).then(res=>{
        if(res.data.code=='0'){
          this.tableData=res.data.data.list,
          this.total=res.data.data.total
        }else{
          this.$message.error("查询失败")
        }
      })
     },
     //分页方法
     handleSizeChange(pageSize) {
      this.pageSize = pageSize
      console.log(pageSize)
      this.getStore()
    },

    handleCurrentChange(pageNum) {
      this.pageNum = pageNum
      this.getStore()
      console.log(pageNum)
    },
    //搜索条件重置
    reset() {
      this.SearchStore.storehouseName = "";
    },
  },
  created() {
    this.store.userId=this.user.data.userId
    this.getStore()
  },
};
</script>

<style lang="less" scoped>
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.text {
  font-size: 20px;
}

.item {
  padding: 18px 0;
}

.addform {
  .el-input {
    // margin-left: 10px;
    width: 70%;
  }
}
</style>